package cn.iocoder.lw.module.core.controller.admin.evaluation;

import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.module.core.controller.admin.evaluation.vo.evaluation.*;
import cn.iocoder.lw.module.core.convert.evaluation.CoreWorkOrderEvaluationConvert;
import cn.iocoder.lw.module.core.dal.dataobject.evaluation.CoreWorkOrderEvaluationDO;
import cn.iocoder.lw.module.core.enums.evaluation.EvaluationLevelEnum;
import cn.iocoder.lw.module.core.service.evaluation.CoreWorkOrderEvaluationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 工单评价")
@RestController
@RequestMapping("/core/work-order-evaluation")
@Validated
public class CoreWorkOrderEvaluationController {

    @Resource
    private CoreWorkOrderEvaluationService evaluationService;

    @PostMapping("/create")
    @Operation(summary = "创建工单评价")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:create')")
    public CommonResult<Long> createEvaluation(@Valid @RequestBody CoreWorkOrderEvaluationSaveReqVO createReqVO) {
        return success(evaluationService.createEvaluation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单评价")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:update')")
    public CommonResult<Boolean> updateEvaluation(@Valid @RequestBody CoreWorkOrderEvaluationSaveReqVO updateReqVO) {
        evaluationService.updateEvaluation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单评价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:delete')")
    public CommonResult<Boolean> deleteEvaluation(@RequestParam("id") Long id) {
        evaluationService.deleteEvaluation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单评价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<CoreWorkOrderEvaluationRespVO> getEvaluation(@RequestParam("id") Long id) {
        CoreWorkOrderEvaluationDO evaluation = evaluationService.getEvaluation(id);
        return success(BeanUtils.toBean(evaluation, CoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单评价分页")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<PageResult<CoreWorkOrderEvaluationRespVO>> getEvaluationPage(@Valid CoreWorkOrderEvaluationPageReqVO pageReqVO) {
        PageResult<CoreWorkOrderEvaluationDO> pageResult = evaluationService.getEvaluationPage(pageReqVO);
        return success(CoreWorkOrderEvaluationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/work-order/{workOrderId}")
    @Operation(summary = "根据工单编号获得评价")
    @Parameter(name = "workOrderId", description = "工单编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<CoreWorkOrderEvaluationRespVO> getEvaluationByWorkOrderId(@PathVariable("workOrderId") Long workOrderId) {
        CoreWorkOrderEvaluationDO evaluation = evaluationService.getEvaluationByWorkOrderId(workOrderId);
        return success(BeanUtils.toBean(evaluation, CoreWorkOrderEvaluationRespVO.class));
    }

    @PostMapping("/reply")
    @Operation(summary = "商家回复评价")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:reply')")
    public CommonResult<Boolean> merchantReply(@Valid @RequestBody CoreWorkOrderEvaluationReplyReqVO replyReqVO) {
        evaluationService.merchantReply(replyReqVO.getId(), replyReqVO.getReply());
        return success(true);
    }

    @PutMapping("/mark-read/{id}")
    @Operation(summary = "标记评价为已读")
    @Parameter(name = "id", description = "评价编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:update')")
    public CommonResult<Boolean> markAsRead(@PathVariable("id") Long id) {
        evaluationService.markAsRead(id);
        return success(true);
    }

    @PutMapping("/batch-mark-read")
    @Operation(summary = "批量标记评价为已读")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:update')")
    public CommonResult<Boolean> batchMarkAsRead(@RequestBody List<Long> ids) {
        evaluationService.batchMarkAsRead(ids);
        return success(true);
    }

    @GetMapping("/initiator/{initiatorId}")
    @Operation(summary = "根据发起人编号获得评价列表")
    @Parameter(name = "initiatorId", description = "发起人编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<List<CoreWorkOrderEvaluationRespVO>> getEvaluationListByInitiatorId(@PathVariable("initiatorId") Long initiatorId) {
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getEvaluationListByInitiatorId(initiatorId);
        return success(BeanUtils.toBean(list, CoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/assignee/{assigneeId}")
    @Operation(summary = "根据接单人编号获得评价列表")
    @Parameter(name = "assigneeId", description = "接单人编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<List<CoreWorkOrderEvaluationRespVO>> getEvaluationListByAssigneeId(@PathVariable("assigneeId") Long assigneeId) {
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getEvaluationListByAssigneeId(assigneeId);
        return success(BeanUtils.toBean(list, CoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/unread")
    @Operation(summary = "获得未读评价列表")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<List<CoreWorkOrderEvaluationRespVO>> getUnreadEvaluations() {
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getUnreadEvaluations();
        return success(BeanUtils.toBean(list, CoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/unread/{assigneeId}")
    @Operation(summary = "获得接单人未读评价列表")
    @Parameter(name = "assigneeId", description = "接单人编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<List<CoreWorkOrderEvaluationRespVO>> getUnreadEvaluationsByAssigneeId(@PathVariable("assigneeId") Long assigneeId) {
        List<CoreWorkOrderEvaluationDO> list = evaluationService.getUnreadEvaluationsByAssigneeId(assigneeId);
        return success(BeanUtils.toBean(list, CoreWorkOrderEvaluationRespVO.class));
    }

    @GetMapping("/statistics")
    @Operation(summary = "获得评价统计信息")
    @Parameter(name = "assigneeId", description = "接单人编号，为空获取全局统计")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<Map<String, Object>> getEvaluationStatistics(@RequestParam(value = "assigneeId", required = false) Long assigneeId) {
        Map<String, Object> statistics = evaluationService.getEvaluationStatistics(assigneeId);
        return success(statistics);
    }

    @GetMapping("/average-score")
    @Operation(summary = "获得平均评分")
    @Parameter(name = "assigneeId", description = "接单人编号，为空获取全局平均分")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<Double> getAverageScore(@RequestParam(value = "assigneeId", required = false) Long assigneeId) {
        Double averageScore = evaluationService.getAverageScore(assigneeId);
        return success(averageScore);
    }

    // ==================== 评价等级枚举 ====================

    @GetMapping("/evaluation-level-list")
    @Operation(summary = "获得评价等级列表")
    @PreAuthorize("@ss.hasPermission('core:work-order-evaluation:query')")
    public CommonResult<List<EvaluationLevelEnum>> getEvaluationLevelList() {
        return success(List.of(EvaluationLevelEnum.values()));
    }

}